using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using Xwltz.Framework.Helper;

namespace Xwltz.Framework.Framework
{
    public class RequestParam
    {
        private readonly NameValueCollection _args;

        public string this[int key] => _args.Count <= key ? string.Empty : _args[key];

        public string this[string key] => _args[key] ?? string.Empty;

        public RequestParam(NameValueCollection collection)
        {
            _args = collection;
        }

        public RequestParam()
        {
            _args = HttpContext.Current.Request.Params;
        }

        public RequestParam(Dictionary<string, string> dic)
        {
            var nameValueCollection = new NameValueCollection();
            foreach (var current in dic.Keys)
            {
                nameValueCollection.Add(current, dic[current]);
            }
            _args = nameValueCollection;
        }

        public int ParamInt(string name, int def = 0)
        {
            return this[name].ToInt(def);
        }

        public float ParamFloat(string name, float def = 0f)
        {
            return this[name].ToFloat(def);
        }

        public string Param(string name, string def = "")
        {
            var text = this[name];
            return !string.IsNullOrEmpty(text) ? text : def;
        }

        public double ParamDouble(string name, double def = 0.0)
        {
            return this[name].ToDouble(def);
        }

        public DateTime ParamTime(string name, DateTime deftime)
        {
            return this[name].ToDateTime(deftime);
        }

        public bool ParamBool(string name, bool def = false)
        {
            return this[name] != null && (this[name] == "on" || this[name].ToBool());
        }

        public bool? ParamBoolNull(string name, bool def = false)
        {
            var text = this[name];
            if (string.IsNullOrWhiteSpace(text) || text.Equals("all"))
            {
                return null;
            }
            return this[name].ToBool();
        }

        public Dictionary<string, string> ToDictionary()
        {
            var dictionary = new Dictionary<string, string>();
            for (var i = 0; i < _args.Count; i++)
            {
                dictionary.Add(_args.GetKey(i), this[i]);
            }
            return dictionary;
        }

        public bool Exist(string name)
        {
            var value = this[name];
            return !string.IsNullOrWhiteSpace(value);
        }

        public bool Add(string key, string value)
        {
            if (string.IsNullOrEmpty(key))
            {
                return false;
            }
            _args.Add(key, value);
            return true;
        }

        public bool Update(string key, string value)
        {
            if (string.IsNullOrEmpty(key))
            {
                return false;
            }
            _args[key] = value;
            return true;
        }

        public bool Remove(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                return false;
            }
            if (!_args.AllKeys.Contains(key)) return false;
            _args.Remove(key);
            return true;
        }
    }
}
